from datetime import datetime
import json
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()


class QuestionBank(db.Model):
    __tablename__ = 'question_bank'
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(255), nullable=False)
    option = db.Column(db.String(255), nullable=True)
    answer = db.Column(db.String(255), nullable=False)
    grade = db.Column(db.String(255), nullable=False)
    score = db.Column(db.Integer, nullable=False)
    type = db.Column(db.String(255), nullable=False)
    day = db.Column(db.Integer, nullable=False)
    subject = db.Column(db.String(255), nullable=False)
    knowledge = db.Column(db.String(255), nullable=False)

    def __repr__(self):
        return'{'+f'id={self.id},title={self.title},option={self.option},answer={self.answer},grad={self.grade},score={self.score},type={self.type},day={self.day},subject={self.subject},knowledge={self.knowledge}'+'}'


class Exam(db.Model):
    __tablename__ = 'exam'
    id = db.Column(db.Integer, primary_key=True)  # id
    subject = db.Column(db.String(255), nullable=False)  # 科目
    name = db.Column(db.String(255), nullable=False)  # 试卷名称
    student_count = db.Column(db.Integer, nullable=False)  # 人数
    teacher_id = db.Column(db.Integer, nullable=False)   # 老师id
    class_id = db.Column(db.Integer, nullable=False)   # 班级id
    exam_type = db.Column(db.String(255), nullable=False)  # 考试类型（如月考、期中）
    start_time = db.Column(db.DateTime, nullable=False)  # 开始时间
    end_time = db.Column(db.DateTime, nullable=False)    # 结束时间
    exam_duration = db.Column(db.Integer, nullable=False)  # 考试时间（分钟）
    status = db.Column(db.String(50), nullable=False)    # 状态（如"未开始""进行中""已结束"）
    testpaper_id = db.Column(db.Integer, nullable=False)  # 试卷ID


    def __repr__(self):
        return f'<Exam id={self.id}, subject={self.subject}, status={self.status}>'

class Class(db.Model):
    __tablename__ = 'class'
    id = db.Column(db.Integer, primary_key=True)
    class_name = db.Column(db.String(255), nullable=False)
    status = db.Column(db.String(255), nullable=False)
    create_date = db.Column(db.DateTime, nullable=False)
    teacher_id = db.Column(db.Integer, nullable=False)
    grade = db.Column(db.String(255), nullable=False)

    def __repr__(self):
        return '{'+fr'id={self.id},class_name={self.class_name},status={self.status},create_data={self.create_date},teacher_id={self.teacher_id}'+'}'


class ExamClass(db.Model):
    __tablename__ = 'exam_class'
    id = db.Column(db.Integer, primary_key=True)  # 中间表ID
    exam_id = db.Column(db.Integer, nullable=False)  # 关联考试ID
    class_id = db.Column(db.Integer, nullable=False)  # 关联班级ID
    join_time = db.Column(db.DateTime, default=datetime.now)  # 班级加入考试的时间

    def __repr__(self):
        return f'<ExamClass exam_id={self.exam_id}, class_id={self.class_id}>'

class Test2question(db.Model):  #Test
    __tablename__ = 'test2question'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255), nullable=False)
    subject = db.Column(db.String(255), nullable=False)

class Test_paper(db.Model):  #ExamQuestion
    __tablename__ = 'test_paper'
    id = db.Column(db.Integer, primary_key=True)
    testpaper_id = db.Column(db.Integer,  nullable=False)
    question_id = db.Column(db.Integer, nullable=False)
    answer = db.Column(db.String(255), nullable=False)  # 答案，最大长度255字符，不可为空
    option = db.Column(db.String(255), nullable=True)  # 选项，最大长度255字符，可为空
    score = db.Column(db.Integer, nullable=False)  # 分值，整型，不可为空
    type = db.Column(db.String(255), nullable=False)  # 题目类型，最大长度255字符，不可为空

class Answer(db.Model):
    __tablename__ = 'answer'
    id = db.Column(db.Integer, primary_key=True)
    exam_id = db.Column(db.Integer, nullable=False)
    student_id = db.Column(db.Integer, nullable=False)
    stu_answer = db.Column(db.Text, nullable=True)
    standard_answer = db.Column(db.Text, nullable=False)
    stu_score = db.Column(db.Float, nullable=False)
    standard_score = db.Column(db.Float, nullable=False)
    question_id = db.Column(db.Integer, nullable=False)
    num = db.Column(db.Integer, nullable=False)
    status = db.Column(db.String(255))


class Teacher(db.Model):
    __tablename__ = 'teacher'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255), nullable=False)
    age = db.Column(db.Integer, nullable=False)
    gender = db.Column(db.String(255), nullable=False)
    job = db.Column(db.String(255), nullable=False)
    password = db.Column(db.String(255), nullable=False)

    def __repr__(self):
        return '{' + fr'id={self.id},name={self.name},age={self.age},gender={self.gender},job={self.job},password={self.password}'+'}'

class Student(db.Model):
    __tablename__ = 'student'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255), nullable=False)
    age = db.Column(db.Integer, nullable=False)
    gender = db.Column(db.String(255), nullable=False)
    class_id = db.Column(db.Integer, nullable=False)
    password = db.Column(db.String(255), nullable=False)

    def __repr__(self):
        return '{' + fr'id={self.id},name={self.name},age={self.age},gender={self.gender},class_id={self.class_id},password={self.password}'+'}'

